package com.tencent.mm.compatible;

import android.text.TextUtils;

import java.lang.reflect.Field;

public class LoaderC<T> {
    private Field bEC;
    private String classname;
    private String fieldName;
    private boolean fxi;
    private Object obj;

    public LoaderC(Object obj, String str, String str2) {
        if (obj == null) {
            throw new IllegalArgumentException("obj cannot be null");
        }
        this.obj = obj;
        this.fieldName = str;
        this.classname = str2;
    }

    private void prepare() {
        if (fxi) return;
        fxi = true;

        Class clazz = obj.getClass();
        while (true) {
            if (clazz == null) break;

            try {
                Field field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                bEC = field;
                break;
            } catch (NoSuchFieldException e1) {
                try {
                    if (!TextUtils.isEmpty(classname)) {
                        Field[] declaredFields = clazz.getDeclaredFields();
                        for (Field field : declaredFields) {
                            Class<?> cls = field.getType();
                            String name = cls.getName();
                            if (TextUtils.equals(classname, name)) {
                                field.setAccessible(true);
                                bEC = field;
                                break;
                            }
                        }
                    }
                } catch (Exception e2) {
                    // ignore
                }
            }
            clazz = clazz.getSuperclass();
        }
    }

    public final boolean Xs() {
        prepare();
        return this.bEC != null;
    }

    public final T get() throws NoSuchFieldException {
        prepare();
        if (this.bEC == null) {
            throw new NoSuchFieldException();
        }
        try {
            return (T) this.bEC.get(this.obj);
        } catch (ClassCastException | IllegalAccessException e) {
            throw new IllegalArgumentException("unable to cast object");
        }
    }
}
